package com.atguigu.gulimall.product.controller;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.common.utils.R;



/**
 * 商品三级分类
 *
 * @author jianPing
 * @email 754844887@qq.com
 * @date 2022-01-14 15:27:51
 */
@Api(value = "商品三级分类")
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @ApiOperation(value = "获取分类树形结构")
    @RequestMapping("/list/tree")
    public R listWithTree(){
        List<CategoryEntity> tree = categoryService.listWithTree();
        return R.ok().put("data", tree);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);
        return R.ok();
    }

    /**
     * 批量修改排序
     */
    @RequestMapping("/update/sort")
    public R batchUpdateSort(@RequestBody List<CategoryEntity> categoryList){
        categoryService.updateBatchById(categoryList);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
        categoryService.updateDetail(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    public R delete(@RequestBody List<Long> catIds){
        categoryService.removeMenuByIds(catIds);
        return R.ok();
    }

}
